Page({
	data: {
		//用户全局信息
		userInfo: {},
    search: '',
    posting:{},
    comments:[],
    postId:'',
    post:null,
		wxavatar1: '/static/avatar.png'
	},
	onLoad(option) {
    const postId = option.id;
    this.setData({
      postId:postId
    })
    this.init(postId);
    this.setData({
      userInfo: app.globalData.userInfo
    })
    console.log(this.data.userInfo)
	},
  onShow() {},
	async init(postId) {
    this.getPostsById(postId); 
    this.getCommentsById(postId);
  },
  async getPostsById(id) {
    console.log(id);
    try {
      const res = await wx.cloud.callFunction({
        name: 'getPostsById',
        data: {
          id: id
        }
      });
      if (res.result.code === 0) {
        this.setData({
          posting: res.result.data.data[0]
        });
        console.log(this.data.posting); // 打印更新后的食物列表数据
      } else {
        console.error('获取食物失败:', res.result.msg);
      }
    } catch (err) {
      console.error('调用云函数时发生错误:', err);
    }
  },
  async getCommentsById(id) {
    console.log(id);
    try {
      const res = await wx.cloud.callFunction({
        name: 'getCommentsById',
        data: {
          id: id
        }
      });
      if (res.result.code === 0) {
        this.setData({
          comments: res.result.data.data
        });
        console.log(this.data.comments); // 打印更新后的食物列表数据
      } else {
        console.error('获取食物失败:', res.result.msg);
      }
    } catch (err) {
      console.error('调用云函数时发生错误:', err);
    }
  },
  async send(){
    console.log(this.data.search)
    try {
      const res = await wx.cloud.callFunction({
        name: 'sendComment',
        data: {
          nickName:this.data.userInfo.nickName,
          avatarUrl:this.data.userInfo.avatarUrl,
          openId:this.data.userInfo._openid,
          postId:this.data.postId,
          text: this.data.search
        }
      });
      if (res.result.code === 0) {
        console.log(res.result); // 打印更新后的食物列表数据
      } else {
        console.error('获取食物失败:', res.result.msg);
      }
    } catch (err) {
      console.error('调用云函数时发生错误:', err);
    }
    wx.reLaunch({
      url: '/pages/forumIntroduce/forumIntroduce?id=' + this.data.postId,
      success: () => {
        console.log('页面重新启动成功');
      },
      fail: (err) => {
        console.error('页面重新启动失败', err);
      }
    });
  },
	chooseWxavatar1(e) {
		const { avatarUrl } = e.detail;
		let baseavatarUrl = 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(avatarUrl, 'base64');
		this.setData({
			wxavatar1: avatarUrl
		});
	},
	async uploadWxavatar1(e) {
		await this.uploadImage(this, 'wxavatar1', 'wxavatar1Datas', '', 1, 'avatar');
	}
});
const app=getApp()
